// 导入request请求工具类
import {
    getBaseUrl,
    requestUtil
} from '../../utils/requestUtil.js';
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 轮播图数组
        swiperList: [],
        baseUrl: '',
        // 商品大类数组
        bigTypeList: [],
        bigTypeList_row1: [],
        bigTypeList_row2: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getSwiperList()
        this.getBigTypeList()
        this.getHotProductList()
    },
    /**
     * 获取轮播图数据
     */
    async getSwiperList() {
        const result = await requestUtil({
            url: '/product/findSwiper',
            method: "GET"
        });
        // 成功的时候 回调
        const baseUrl = getBaseUrl();
        this.setData({
            swiperList: result.message,
            baseUrl
        })
    },
    /**
     * 获取商品大类
     */
    async getBigTypeList() {
        const result = await requestUtil({
            url: '/bigType/findAll',
            method: "GET"
        });
        const bigTypeList = result.message;
        // 第一行 1-5
        const bigTypeList_row1 = bigTypeList.filter((item, index) => {
            return index < 5;
        })
        // 第二行 6-10
        const bigTypeList_row2 = bigTypeList.filter((item, index) => {
            return index >= 5;
        })
        this.setData({
            bigTypeList,
            bigTypeList_row1,
            bigTypeList_row2,
        })
    },
    /**
     * 获取热卖商品
     */
    async getHotProductList() {
        const result = await requestUtil({
            url: '/product/findHot',
            method: "GET"
        });
        this.setData({
            hotProductList: result.message
        })
    },
    // 商品大类点击跳转对应的分类页
    handleTypeJump(e){
        const {index} = e.currentTarget.dataset;
        const app = getApp();
        app.globalData.index = index;
        wx.switchTab({
          url: '/pages/category/index',
        })
    }
})